/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.futco.sysconthos.Modelo.Procesos.Daos;

import org.futco.sysconthos.Modelo.Admisiones.Daos.IDAO;
import org.futco.sysconthos.Modelo.Admisiones.Daos.Model;
import org.futco.sysconthos.Modelo.Proceso.Tratamiento;
import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author sala4
 */
public class TratamientoDAO implements IDAO<Tratamiento>{
      private Model model;
    private PreparedStatement psInsert;

    public TratamientoDAO() throws SQLException {
        model = Model.getInstance();
    }

    public void insert(Tratamiento t) throws SQLException {
       if (psInsert == null) {
            String sqlInsert = "INSERT INTO tratamiento  VALUES (?,?,?,?,?,?,?)";
            psInsert = (PreparedStatement) model.getCon().prepareStatement(sqlInsert);

        java.sql.Date fH = new java.sql.Date(t.getFecha().getTime());

        psInsert.setString(1, t.getCodigo());
        psInsert.setDouble(2, t.getCosto_total());
        psInsert.setString(3,t.getTipo());
        psInsert.setDate(4, fH);
        psInsert.setString(5,t.getMedicamentosPrescripciones());
        psInsert.setString(6, t.getHojaEvolucion());
        psInsert.setString(7, t.getCodHC());

        
        psInsert.executeUpdate();
        }
    }
     public Tratamiento load(ResultSet rs) throws SQLException{
          Tratamiento tratam = new Tratamiento();

          tratam.setCodigo(rs.getString(1));
          tratam.setCosto_total(rs.getInt(2));
          tratam.setTipo(rs.getString(3));
          tratam.setFecha(rs.getDate(4));
          tratam.setMedicamentosPrescripciones(rs.getString(5));
          tratam.setHojaEvolucion(rs.getString(6));
          tratam.setCodHC(rs.getString(7));

        return tratam;
    }

    public void delete(Tratamiento t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

       public List<Tratamiento> getAll() throws SQLException {
        List lista = new LinkedList<Tratamiento>();
        java.sql.PreparedStatement pst = null;
        ResultSet rs = null;
        Tratamiento trata = null;
        if(!model.isConnect())
        {
            throw new SQLException(":V  historia");
        }
        try{
            pst = model.getCon().prepareStatement("SELECT * FROM pago");
            rs = pst.executeQuery();
            if(rs==null){
                throw new SQLException("No hay Pago registrado");
            }
            while(rs.next())
            {
                lista.add(load(rs));
            }
        } finally {
            if (rs != null) {
               rs.close();
           }
            if (pst != null) {
              pst.close();
           }
       }

        return lista;
    }

    public void update(Tratamiento t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

   public List<Tratamiento> getAllBy(String parametro, String nombre) throws SQLException {

        List lista = new LinkedList<Tratamiento>();
        java.sql.PreparedStatement pst = null;
        ResultSet rs = null;
        Tratamiento trat = null;


        try {
            pst = model.getCon().prepareStatement("SELECT * FROM tratamiento WHERE " +parametro + " like ?");
            pst.setString(1, "%" + nombre.trim() + "%");
            rs = pst.executeQuery();

            while (rs.next()) {
                lista.add(load(rs));

            }

        } finally {
            if (pst != null) {
                pst.close();
            }
            if (rs != null) {
                rs.close();
            }
        }
        return lista;


    }

      public Tratamiento find(String cod) throws SQLException {

        java.sql.PreparedStatement pst =  null;
        Tratamiento tr  = null;
        ResultSet rs = null;
        if(!model.isConnect())
        {
            throw new SQLException("no hay coneccion");
        }
        try{
            pst = model.getCon().prepareStatement("Select * from tratamiento where codigo = ?");
            pst.setString(1, cod);
            rs = pst.executeQuery();
            if(rs.next())
            {
                tr = load(rs);

            }
        }finally{
            if(rs != null && pst !=null)
            {
                pst.close();
                rs.close();
            }
        }
        return tr;
    }
    public Tratamiento consultarporCodHC(String cod) throws SQLException {

        java.sql.PreparedStatement pst =  null;
        Tratamiento tr  = null;
        ResultSet rs = null;
        if(!model.isConnect())
        {
            throw new SQLException("no hay coneccion");
        }
        try{
            pst = model.getCon().prepareStatement("Select * from tratamiento where codHc = ?");
            pst.setString(1, cod.trim());
            rs = pst.executeQuery();
            if(rs.next())
            {
                tr = load(rs);
            }
        }finally{
            if(rs != null && pst !=null)
            {
                pst.close();
                rs.close();
            }
        }
        return tr;
    }

    public Tratamiento find(Integer id) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void update(String codigo, Tratamiento t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
